<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/s_l/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/s_l/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/s_l/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/s_l/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/s_l/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/s_l/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/s_l/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Java基础,">










<meta name="description" content="面向对象是一种编程思想，而类和对象是一种技术实现，而想要真正的理解面向对象为什么是一种编程思想，需要一定的程序开发经验">
<meta name="keywords" content="Java基础">
<meta property="og:type" content="article">
<meta property="og:title" content="面向对象思想概述-封装">
<meta property="og:url" content="http://yoursite.com/2019/04/18/面向对象思想的概述-封装/index.html">
<meta property="og:site_name" content="S.L">
<meta property="og:description" content="面向对象是一种编程思想，而类和对象是一种技术实现，而想要真正的理解面向对象为什么是一种编程思想，需要一定的程序开发经验">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-04-18T13:49:28.161Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="面向对象思想概述-封装">
<meta name="twitter:description" content="面向对象是一种编程思想，而类和对象是一种技术实现，而想要真正的理解面向对象为什么是一种编程思想，需要一定的程序开发经验">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/s_l/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/04/18/面向对象思想的概述-封装/">





  <title>面向对象思想概述-封装 | S.L</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/s_l/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">S.L</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">记录学习过程中的点滴</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/s_l/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/s_l/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/s_l/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/s_l/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/s_l/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/s_l/2019/04/18/面向对象思想的概述-封装/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏立鹏">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/s_l/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="S.L">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">面向对象思想概述-封装</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-18T21:01:34+08:00">
                2019-04-18
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.4k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>面向对象是一种编程思想，而类和对象是一种技术实现，而想要真正的理解面向对象为什么是一种编程思想，需要一定的程序开发经验</p>
</blockquote>
<a id="more"></a>
<hr>
<h2 id="对象与类"><a href="#对象与类" class="headerlink" title="对象与类"></a>对象与类</h2><p>   类是对现实世界事物的抽象模板<br>   对象是对现实世界具体事物的具体实现，通过类模板构造，使用OOP思想一定要明白每一个对象都具有</p>
<ul>
<li>对象的行为：可以对对象施加哪些操作，即对象的方法调用</li>
<li>对象的状态：当施加方法时，对象如何响应</li>
<li>对象标识：如何辨别具有相同行为与状态的不同对象？<br>每一个对象在程序中都是独一无二的，就如同每一个人在这个世界上都具有不可替代性，如果把每一对象都理解为真实存在的人，就可以明白对象的的行为和状态。    </li>
</ul>
<h2 id="类之间的关系"><a href="#类之间的关系" class="headerlink" title="类之间的关系"></a>类之间的关系</h2><p>   类之间最常见的关系为依赖，聚合，继承。暂时超过本章内容，不作讨论。</p>
<h2 id="自定义类"><a href="#自定义类" class="headerlink" title="自定义类"></a>自定义类</h2><p>   与自定义类有一个区别概念是系统的预定义类，属于java系统中的API中的所谓类库的内容。暂时不涉及使用类库中的内容，所以只研究。</p>
<h3 id="类的构成"><a href="#类的构成" class="headerlink" title="类的构成"></a>类的构成</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Employee</span></span>&#123;</span><br><span class="line">		<span class="keyword">private</span> String name;</span><br><span class="line">		<span class="keyword">private</span> <span class="keyword">double</span> salary;</span><br><span class="line">		</span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="title">Employee</span><span class="params">(String name, <span class="keyword">double</span> salary)</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">			<span class="keyword">this</span>.name = name;</span><br><span class="line">			<span class="keyword">this</span>.salary = salary;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			<span class="keyword">return</span> name;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">			<span class="keyword">this</span>.name = name;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">getSalary</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			<span class="keyword">return</span> salary;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSalary</span><span class="params">(<span class="keyword">double</span> salary)</span> </span>&#123;</span><br><span class="line">			<span class="keyword">this</span>.salary = salary;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">raiseSalary</span><span class="params">(<span class="keyword">double</span> byPercent)</span> </span>&#123;</span><br><span class="line">			<span class="keyword">double</span> raise=salary*byPercent/<span class="number">100</span>;</span><br><span class="line">			salary+=raise;</span><br><span class="line">		&#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>从上面代码中可以看出一个类的基本内容，即类中一般包含成员变量如name，salary字段，包含一个构造器，可以看到构造器与类同名，在构造Employee时候，构造器会运行，以便将实例域初始化，构造器拥有以下特征</p>
<h4 id="构造器的特征"><a href="#构造器的特征" class="headerlink" title="构造器的特征"></a>构造器的特征</h4><ul>
<li>构造器与类同名</li>
<li>每个类可以有一个以上的构造器</li>
<li>构造器可以有0个或多个参数</li>
<li>构造器没有返回值</li>
<li>构造器总是伴随着new操作符一起使用</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//构造器与类名相同无返回值</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">Employee</span><span class="params">(String name, <span class="keyword">double</span> salary)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">this</span>.name = name;</span><br><span class="line">			<span class="keyword">this</span>.salary = salary;</span><br><span class="line">		&#125;</span><br></pre></td></tr></table></figure>
<p>包含类的成员方法，如raiseSalary()方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">getSalary</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> salary;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSalary</span><span class="params">(<span class="keyword">double</span> salary)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.salary = salary;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">raiseSalary</span><span class="params">(<span class="keyword">double</span> byPercent)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">double</span> raise=salary*byPercent/<span class="number">100</span>;</span><br><span class="line">		salary+=raise;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h4 id="封装的优点"><a href="#封装的优点" class="headerlink" title="封装的优点"></a>封装的优点</h4><p>   接下来我们看一下封装的代码<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">     <span class="keyword">return</span> name;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">     <span class="keyword">this</span>.name = name;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">getSalary</span><span class="params">()</span> </span>&#123;</span><br><span class="line">     <span class="keyword">return</span> salary;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSalary</span><span class="params">(<span class="keyword">double</span> salary)</span> </span>&#123;</span><br><span class="line">     <span class="keyword">this</span>.salary = salary;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure></p>
<p>封装的优点在于可以封装出一个get访问器和set访问器</p>
<ul>
<li>保证了数据的安全性，不允许直接调用实例化后的字段读写值</li>
<li>增强了数据校验，可以在set访问器中限制写入数据的大小</li>
<li>在get访问器中，可以格式化返回字符串输出，方便一些如日期转化，小数点精确转化的内容</li>
</ul>
<h4 id="this关键字"><a href="#this关键字" class="headerlink" title="this关键字"></a>this关键字</h4><p>   this关键字比较难以理解，因为在类中使用this关键字代表的是当前调用对象的实例，对于这一点确实比较难以理解，因为this并非固定的对象，而是调用者是谁，则指向谁，不过可以简单理解为就是当前调用对象</p>
<h4 id="基于类的访问权限"><a href="#基于类的访问权限" class="headerlink" title="基于类的访问权限"></a>基于类的访问权限</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Employee boss)</span> </span>&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">this</span>.name.equals(boss.name);</span><br><span class="line">		&#125;</span><br></pre></td></tr></table></figure>
<p>观察上述方法，发现一个问题，equals()方法直接使用了boss的私有域的name，正常理解不是应该通过getName()方法调用，返回得到name值的，但是这里是合法的这是基于类的访问权限，Employee类的方法可以访问Employee类的任何一个对象的私有域</p>
<h4 id="final实例域"><a href="#final实例域" class="headerlink" title="final实例域"></a>final实例域</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Employee</span></span>&#123;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> String name;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>观察上述代码可以看到一个新的关键字final，被final修饰的关键字只有get获取器，即为只读的字段，不可以写入。</p>
<h2 id="类设计技巧"><a href="#类设计技巧" class="headerlink" title="类设计技巧"></a>类设计技巧</h2><h3 id="一定要保证数据私有"><a href="#一定要保证数据私有" class="headerlink" title="一定要保证数据私有"></a>一定要保证数据私有</h3><p>  类设计一定 要保证数据私有，这是防止对象实例化后被攻击的最好方法，一定要对类的数据进行私有化！！！</p>
<h3 id="不要类中使用过多的基本类型"><a href="#不要类中使用过多的基本类型" class="headerlink" title="不要类中使用过多的基本类型"></a>不要类中使用过多的基本类型</h3><p>   通常一个类也可以作为一个类成员变量（属性），所以尽可能的多使用类代替基本类型的成员变量，这样才更加符合oop对类的设计。</p>
<h3 id="不是所有的域都需要get访问器和set访问器"><a href="#不是所有的域都需要get访问器和set访问器" class="headerlink" title="不是所有的域都需要get访问器和set访问器"></a>不是所有的域都需要get访问器和set访问器</h3><p>   有些私有成员变量为只读的，比如使用final关键字修饰的字段，就不需要set访问器</p>
<h3 id="将职责过多的类分解"><a href="#将职责过多的类分解" class="headerlink" title="将职责过多的类分解"></a>将职责过多的类分解</h3><p>   一个类不要具有太多功能，举一个具体的例子，比如要实现一个订单系统，那么商品订单模块和产品模块就要分开，通常订单模块还会被分为好几个类共同实现订单功能</p>
<h3 id="类名称和方法名称要能够体现功能"><a href="#类名称和方法名称要能够体现功能" class="headerlink" title="类名称和方法名称要能够体现功能"></a>类名称和方法名称要能够体现功能</h3><p>   比如订单类的名称应该为Order这样所有人都知道这是一个订单，而不是用O作为类名，造成定义模糊</p>
<h3 id="优先使用不可变类"><a href="#优先使用不可变类" class="headerlink" title="优先使用不可变类"></a>优先使用不可变类</h3><p>不可变类暂时不会接触，不作介绍</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/s_l/tags/Java基础/" rel="tag"># Java基础</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/s_l/2019/04/18/Java的数据类型和方法设计/" rel="next" title="Java的数据类型和方法设计">
                <i class="fa fa-chevron-left"></i> Java的数据类型和方法设计
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/s_l/2019/05/06/线程的四种创建方式/" rel="prev" title="线程的四种创建方式">
                线程的四种创建方式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏立鹏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/s_l/archives/">
              
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/www.86740.com/projects" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#对象与类"><span class="nav-number">1.</span> <span class="nav-text">对象与类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类之间的关系"><span class="nav-number">2.</span> <span class="nav-text">类之间的关系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自定义类"><span class="nav-number">3.</span> <span class="nav-text">自定义类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#类的构成"><span class="nav-number">3.1.</span> <span class="nav-text">类的构成</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#构造器的特征"><span class="nav-number">3.1.1.</span> <span class="nav-text">构造器的特征</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#封装的优点"><span class="nav-number">3.1.2.</span> <span class="nav-text">封装的优点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#this关键字"><span class="nav-number">3.1.3.</span> <span class="nav-text">this关键字</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#基于类的访问权限"><span class="nav-number">3.1.4.</span> <span class="nav-text">基于类的访问权限</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#final实例域"><span class="nav-number">3.1.5.</span> <span class="nav-text">final实例域</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类设计技巧"><span class="nav-number">4.</span> <span class="nav-text">类设计技巧</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一定要保证数据私有"><span class="nav-number">4.1.</span> <span class="nav-text">一定要保证数据私有</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#不要类中使用过多的基本类型"><span class="nav-number">4.2.</span> <span class="nav-text">不要类中使用过多的基本类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#不是所有的域都需要get访问器和set访问器"><span class="nav-number">4.3.</span> <span class="nav-text">不是所有的域都需要get访问器和set访问器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将职责过多的类分解"><span class="nav-number">4.4.</span> <span class="nav-text">将职责过多的类分解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#类名称和方法名称要能够体现功能"><span class="nav-number">4.5.</span> <span class="nav-text">类名称和方法名称要能够体现功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优先使用不可变类"><span class="nav-number">4.6.</span> <span class="nav-text">优先使用不可变类</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏立鹏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/s_l/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/s_l/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/s_l/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/s_l/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/s_l/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/s_l/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/s_l/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/s_l/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/s_l/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
